package db;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class V_Query{

    public static List<String[]> getViewList(String viewName, Connection conn) throws SQLException{
        
        final String sql= " SELECT * FROM " + viewName;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        List<String[]> results = new ArrayList<String[]>();
        try {
	        stmt = conn.prepareStatement(sql);
	        rs = stmt.executeQuery();

	        ResultSetMetaData resultSetMetaData =  rs.getMetaData();
	        int count = resultSetMetaData.getColumnCount();
    		while (rs.next()) {
    			String[] row = new String[count];
    			for (int i = 1; i <= count; i++){
    				row[i-1] = rs.getString(i);
    			}
    			results.add(row);
            }
    		return results;
        } finally{
        	if (stmt != null) {
        		stmt.close();
        	}
        	
        	if (rs != null) {
        		rs.close();
        	}
        }
    }
    
    public static ResultSet getViewResultSet(String viewName, Connection conn) throws SQLException{
        
        final String sql= " SELECT * FROM " + viewName;
        PreparedStatement stmt = conn.prepareStatement(sql);
        ResultSet rs = stmt.executeQuery();
        return rs;
    }
}
